package com.nxy.cpfsfe.service.factory;

import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 短信验证处理
 * Created by lcq on 2019/12/24.
 */
public class SmsgIdfyCdServer implements ForwardServer{
    private static  SmsgIdfyCdServer server = new SmsgIdfyCdServer();
    private SmsgIdfyCdServer(){}
    public static SmsgIdfyCdServer  getInstance(){
        return server;
    }

    private final static Logger LOGGER = LoggerFactory.getLogger(SmsgIdfyCdServer.class);

    @Override
    public String buildEsbXml(Map<String, String> map) {
        LOGGER.info("---------短信验证申请报文CBS9010010--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE, SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CBS" + SysDef.FILE_SP + "CBS_9010010.xml");
            //报文头
            XmlCBSBuildUtil.buildXmlRequestHead(xmlReqDoc, MsgCdOrTpEnum.CBS_9010010.getCode(), map);
            //报文体
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/mobileNo ",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerMobileNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/mobileType",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerMobileType")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/use",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerUse")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/openBrchID",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerBrnchId")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/payCardAcctNb",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerAcctNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/option", StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/Option")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/channel",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/Channel")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("短信验证申请报文CBS9010010失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "短信验证申请报文CBS9010010失败");
        }
    }

    @Override
    public String handleRpsMsg(Map<String, String> map, String respMsg) {
        LOGGER.info("---------短信验证申请报文CBS9010010--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE, SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CPFS" + SysDef.FILE_SP + "CPFS_301.xml");
            //报文头
            XmlCPFSBuildUtil.buildXmlResponseHead(xmlReqDoc, MsgCdOrTpEnum.MSGTD_301.getCode(), map);
            //解析cbs9010011报文
            if(StringUtils.isNotBlank(respMsg)){
                Map<String,String> rpsmap = XmlUtil.xml2Map(respMsg);
                String  retCd = StringUtil.trimObject(rpsmap.get("/transaction/header/status/retCd"));
                //返回码前两位为00代表成功
                if(retCd.startsWith("00")&&retCd.length() ==6){
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/body/response/desc")));
                    map.put("resultCode",StringUtil.trimObject(rpsmap.get("/transaction/body/response/retCd")));
                    map.put("msgNb",StringUtil.trimObject(rpsmap.get("/transaction/body/response/msgNb")));
                }else{
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                }
            }else{
                LOGGER.error("cbs9010011回执报文为空，请联系");
                map.put("msg", "cbs9010011回执报文为空，请联系");
                map.put("resultCode",RespCd.NCPO0005);
            }
            //报文体
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/TrxStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsDesc",  StringUtil.trimObject(map.get("msg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/MsgNb",  StringUtil.trimObject(map.get("msgNb")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("短信验证申请报文CBS9010010失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "短信验证申请报文CBS9010010失败");
        }
    }

}
